<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Programs extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('upload');

	}

	public function index(){
	  	$this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  	$this->load->view($this->session->userdata('formain'),array(
	      'mods'    => $this->wus->getMod(),
	      'glLookups'   => $this->wus->getLookupValues()
	    ));
	  	$this->load->view('layouts/footer');
	 }
	 
	 public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
	      'mods'         => $this->wus->getMod(),
	      'glLookups'    => $this->wus->getLookupValues(),
	      'modules'      => $this->wus->getModName(),
	      'modulemenu'   => $this->wus->getModuleMenus(),
	      'forms'        => $this->wus->getForms(),
	      'formfunction' => $this->wus->getFormFunctions(),
	      'prog_type'	 => $this->programs_model->getlookup('PROGRAM_TYPE'),
	      'data_type'	 => $this->programs_model->getlookup('DATATYPES')
	    ));
	}

  public function edit_form($id){
		//$query = $this->programs_model->editData($id);
		$this->load->view($this->session->userdata('foredit'),array(
		  'program'	     => $this->programs_model->get_data($id),
		  'mods'         => $this->wus->getMod(),
		  'glLookups'    => $this->wus->getLookupValues(),
		  'modules'      => $this->wus->getModName(),
		  'modulemenu'   => $this->wus->getModuleMenus(),
		  'forms'        => $this->wus->getForms(),
		  'formfunction' => $this->wus->getFormFunctions(),
		  'prog_type'	 => $this->programs_model->getlookup('PROGRAM_TYPE'),
		  'data_type'	 => $this->programs_model->getlookup('DATATYPES')
	    ));
	}
	
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
	      'mods'      => $this->wus->getMod(),
	      'program'	     => $this->programs_model->get_data($id),
	      'modulemenu'   => $this->wus->getModuleMenus(),
	      'data_type'	 => $this->programs_model->getlookup('DATATYPES'),
	      'params'		 => $this->programs_model->getparams($id)
	    ));
	}

	public function searchmodmenu(){
		$query = $this->wus->getModuleMenu($this->input->post('mod_menu_id'));
		echo json_encode($query);
	}

	public function searchFormData()
	{
		$fcode = $this->input->post('form-code-s');
		$fname = $this->input->post('form-name-s');
		$mname = $this->input->post('module-name');
		$efn   = $this->input->post('exe-file-name-s');
		$query = $this->programs_model->getData($fcode, $fname, $mname, $efn);
		echo json_encode($query);
	}
	
	public function save(){

		$data = array(
    		':p_program_code' 		=> $this->input->post('pcode'),
    		':p_program_name' 		=> $this->input->post('pname'),
    		':p_description'		=> $this->input->post('desc'),
    		':p_module_id'			=> $this->input->post('modID'),
    		':p_group_id'			=> $this->input->post('ppGroup'),
    		':p_prog_type' 			=> $this->input->post('pType'),
    		':p_exe_file_name'		=> $this->input->post('exename'),
    		':p_enable_flag'	 	=> ($this->input->post('enabled') == 'ok' ? 'Y' : 'N'),
    		':p_created_by'			=> $this->session->userdata('user_id')
    	);

		$this->programs_model->save_program($data);
    	
	}

	public function update(){

		$data = array(
			':p_program_id' 		=> $this->input->post('pid'),
    		':p_program_code' 		=> $this->input->post('pcode'),
    		':p_program_name' 		=> $this->input->post('pname'),
    		':p_description'		=> $this->input->post('desc'),
    		':p_module_id'			=> $this->input->post('modID'),
    		':p_group_id'			=> $this->input->post('ppGroup'),
    		':p_prog_type' 			=> $this->input->post('pType'),
    		':p_exe_file_name'		=> $this->input->post('exename'),
    		':p_enable_flag'	 	=> ($this->input->post('enabled') == 'ok' ? 'Y' : 'N'),
    		':p_updated_by'			=> $this->session->userdata('user_id')
    	);
		$this->programs_model->update_program($data);	
	}

	public function saveparam(){
		$data = array(
			'program_id'	=> $this->input->post('program_id'),
			'seq_no'	    => $this->input->post('seq_no'),
			'program_name'	=> $this->input->post('name'),
			'description'	=> $this->input->post('desc'),
			'data_type'	    => $this->input->post('data_type'),
			'user_id'		=> $this->session->userdata('user_id')
		);
		$this->programs_model->insert_param($data);
	}

	public function saveparamedit(){
		$data = array(
			'program_id'	=> $this->input->post('param_id'),
			'seq_no'	    => $this->input->post('seq_no'),
			'program_name'	=> $this->input->post('name'),
			'description'	=> $this->input->post('desc'),
			'data_type'	    => $this->input->post('data_type'),
			'user_id'		=> $this->session->userdata('user_id')
		);
		$this->programs_model->update_param($data);
	}

	public function getparams(){
		echo json_encode($this->programs_model->getparams($this->input->post('id')));
	}

	public function getparamsbyid(){
		echo json_encode($this->programs_model->getparamsbyid($this->input->post('id')));
	}

	public function deleteparambyid(){
		echo json_encode($this->programs_model->deleteparambyid($this->input->post('id')));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */